//
//  EnergyShopListCell.m
//  Ginix
//
//  Created by 冯志强 on 2025/3/27.
//

#import "EnergyShopListCell.h"
#import "EnergyShopListModel.h"
#import "EnergyListCell.h"

@interface EnergyShopListCell()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong) UICollectionView * listView;
@property (nonatomic,strong) NSMutableArray * listArray;

@end

@implementation EnergyShopListCell

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setSubViewLayouyt];
        [self requestListData];
    }
    return self;
}
-(void)setSubViewLayouyt{
    
    self.backgroundColor = UIColor.clearColor;
    self.contentView.backgroundColor = UIColor.clearColor;
    [self.contentView addSubview:self.listView];
    
}

-(void)requestListData{
    
    NSDictionary * parm = @{@"pageNub":@(1),@"pageSize":@(10),@"type":@(0)};
    [SPNetworkingIns POST:PORT(@"monetary-center/api/monetary/getExchangeProductListNew") params:parm success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject, NSInteger code) {
        NSArray * dataArr = [[responseObject objectForKey:@"data"] objectForKey:@"list"];
        if (dataArr) {
            for (NSDictionary * item in dataArr) {
                EnergyShopListModel * model = [[EnergyShopListModel alloc]init];
                model = [EnergyShopListModel yy_modelWithDictionary:item];
                [self.listArray addObject:model];
            }
        }
        [self.listView reloadData];
    } fail:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        
    }];
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return self.listArray.count;
}

-(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    EnergyListCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"EnergyListCell" forIndexPath:indexPath];
    EnergyShopListModel * model = self.listArray[indexPath.row];
    if (model) {
        [cell setInfoModel:model];
    }

    return cell;
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{

    return UIEdgeInsetsMake(10* FMScaleX, 20* FMScaleX, 10* FMScaleX, 20* FMScaleX);
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    EnergyShopListModel * model = self.listArray[indexPath.row];
    if (model) {
        FhudShowStr(@"积分不足无法参与改活动")
    }
}

-(UICollectionView *)listView{

    if (!_listView) {
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
        layout.minimumLineSpacing = 20 * FMScaleX;
        layout.minimumInteritemSpacing = 20 * FMScaleX;
        layout.itemSize = CGSizeMake(134 * FMScaleX, 241 * FMScaleX);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _listView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 10 * FMScaleX, WIDTH, 270 * FMScaleX) collectionViewLayout:layout];
        _listView.delegate = self;
        _listView.dataSource = self;
        _listView.backgroundColor = UIColor.clearColor;
        _listView.showsVerticalScrollIndicator = NO;
        _listView.showsHorizontalScrollIndicator = NO;
        [_listView registerClass:[EnergyListCell class] forCellWithReuseIdentifier:@"EnergyListCell"];
    }
    return _listView;
}

-(NSMutableArray *)listArray{
    
    if (!_listArray) {
        _listArray = [[NSMutableArray alloc]init];
    }
    return _listArray;
}
@end
